package com.hp.common.dauth;

import com.hp.common.tool.StringTool;
import org.apache.ibatis.cache.CacheKey;
import org.apache.ibatis.executor.Executor;
import org.apache.ibatis.mapping.BoundSql;
import org.apache.ibatis.mapping.MappedStatement;
import org.apache.ibatis.mapping.ParameterMapping;
import org.apache.ibatis.mapping.SqlSource;
import org.apache.ibatis.plugin.*;
import org.apache.ibatis.session.ResultHandler;
import org.apache.ibatis.session.RowBounds;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.Properties;

@Intercepts({@Signature(
        type = org.apache.ibatis.executor.Executor.class,
        method = "query",
        args = {MappedStatement.class,Object.class, RowBounds.class, ResultHandler.class}
),@Signature(
        type = Executor.class,
        method = "query",
        args = {MappedStatement.class,Object.class,RowBounds.class,ResultHandler.class, CacheKey.class, BoundSql.class}
)})
public class DataScopeInterceptor implements Interceptor {

    private static final Logger logger = LoggerFactory.getLogger(DataScopeInterceptor.class);

    @Override
    public Object intercept(Invocation invocation) throws Throwable {
        String dataAuthSql = DataScopeHelper.getLocalDataAuthSql();
        if(StringTool.isNotEmpty(dataAuthSql)){
            Object[] args = invocation.getArgs();
            MappedStatement ms = (MappedStatement) args[0];
            Object parameter = args[1];
            RowBounds rowBounds = (RowBounds) args[2];
            ResultHandler resultHandler = (ResultHandler) args[3];
            Executor executor = (Executor) invocation.getTarget();
            CacheKey cacheKey;
            BoundSql boundSql;
            if(args.length == 4){
                boundSql = ms.getBoundSql(parameter);
                cacheKey = executor.createCacheKey(ms,parameter,rowBounds,boundSql);
            }else {
                cacheKey = (CacheKey) args[4];
                boundSql = (BoundSql) args[5];
            }
            String newSql = boundSql.getSql()+dataAuthSql;
            BoundSql newBoundSql = new BoundSql(ms.getConfiguration(),newSql,boundSql.getParameterMappings(),boundSql.getParameterObject());
            MappedStatement newMs =  newMappedStatement(ms,new BoundSqlSqlSoueur(newBoundSql));
            for(ParameterMapping mapping : boundSql.getParameterMappings()){
                String prop = mapping.getProperty();
                if(boundSql.hasAdditionalParameter(prop)){
                    newBoundSql.setAdditionalParameter(prop,boundSql.getAdditionalParameter(prop));
                }
            }
            return executor.query(newMs,parameter,rowBounds,resultHandler,cacheKey,newBoundSql);
        }else {
            return invocation.proceed();
        }
    }

    @Override
    public Object plugin(Object target) {
        return Plugin.wrap(target,this);
    }

    @Override
    public void setProperties(Properties properties) {
       logger.debug(properties.toString());
    }

    /**
     * 定义一个内部辅助类，作用是包装sql
     */
    class BoundSqlSqlSoueur implements SqlSource{
        private BoundSql boundSql;

        public BoundSqlSqlSoueur(BoundSql boundSql) {
            this.boundSql = boundSql;
        }

        @Override
        public BoundSql getBoundSql(Object o) {
            return boundSql;
        }
    }

    public  MappedStatement newMappedStatement(MappedStatement ms,SqlSource newSqlSource) {
        MappedStatement.Builder builder = new MappedStatement.Builder(ms.getConfiguration(),ms.getId(),newSqlSource,ms.getSqlCommandType());
        builder.resource(ms.getResource());
        builder.fetchSize(ms.getFetchSize());
        builder.statementType(ms.getStatementType());
        builder.keyGenerator(ms.getKeyGenerator());
        if(ms.getKeyProperties() != null && ms.getKeyProperties().length > 0){
            builder.keyProperty(ms.getKeyProperties()[0]);
        }
        builder.timeout(ms.getTimeout());
        builder.parameterMap(ms.getParameterMap());
        builder.resultMaps(ms.getResultMaps());
        builder.resultSetType(ms.getResultSetType());
        builder.cache(ms.getCache());
        builder.flushCacheRequired(ms.isFlushCacheRequired());
        builder.useCache(ms.isUseCache());
        return builder.build();
    }
}
